import request from "../../utils/request";
import PubSub from 'pubsub-js';
let startY = 0; // 手指起始的坐标
let moveY = 0; // 手指移动的坐标
let moveDistance = 0; // 手指移动的距离
Page({

  /**
   * 页面的初始数据
   */
  data: {
    coverTransform: 'translateY(0)',
    coveTransition: '',
    userInfo: {}, // 用户信息
    recentPlayList: [], // 用户播放记录
    index:0,
    recommendList:[],
    showButtom:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 读取用户的基本信息
    let userInfo = wx.getStorageSync('userInfo');
    if(userInfo){ // 用户登录
      // 更新userInfo的状态
      this.setData({
        userInfo: JSON.parse(userInfo),
        showButtom: true
      })   
      // 获取用户播放记录
      this.getUserRecentPlayList(this.data.userInfo.userId)
      // 订阅来自songDetail页面发布的消息
      PubSub.subscribe('switchType', (msg, type) => {
        let {recommendList, index} = this.data;
        if(type === 'pre'){ // 上一首
          (index === 0) && (index = recommendList.length);
          index -= 1;
        }else { // 下一首
          (index === recommendList.length - 1) && (index = -1);
          index += 1;
        }
        
        // 更新下标
        this.setData({
          index
        })
        
        let musicId = recommendList[index].id;
        // 将musicId回传给songDetail页面
        PubSub.publish('musicId', musicId)
        
        });
  }
  },
  // 获取用户播放记录的功能函数
  async getUserRecentPlayList(userId){
  let recentPlayListData = await request('/user/record', {uid: userId, type: 0});
    let index = 0;
    let recentPlayList = recentPlayListData.allData.splice(0, 10).map(item => {
      item.id = index++;
      return item;
    })
    
    let {recommendList} = this.data

    recentPlayList.forEach(item => {
      recommendList.push(item.song)
  })
    this.setData({
      recentPlayList,
      recommendList
    })
  },
  
  handleTouchStart(event){
    this.setData({
      coveTransition: ''
    })
    // 获取手指起始坐标
    startY = event.touches[0].clientY;
  },
  handleTouchMove(event){
    moveY = event.touches[0].clientY;
    moveDistance = moveY - startY;
    
    if(moveDistance <= 0){
      return;
    }
    if(moveDistance >= 80){
      moveDistance = 80;
    }
    // 动态更新coverTransform的状态值
    this.setData({
      coverTransform: `translateY(${moveDistance}rpx)`
    })
  },
  handleTouchEnd(){
    // 动态更新coverTransform的状态值
    this.setData({
      coverTransform: `translateY(0rpx)`,
      coveTransition: 'transform 1s linear'
    })
  },
  
  // 跳转至登录login页面的回调
  toLogin(){
	 // 读取用户的基本信息
	 let userInfo = wx.getStorageSync('userInfo');
	 if(!userInfo){
		wx.navigateTo({
			url: '/pages3/login/login'
		  })
	 } 
  },
  goBoFang(e){
    let {song, index} =e.currentTarget.dataset
		this.setData({
			index
		})
		wx.navigateTo({
		  url: '/pages2/songDetail/songDetail?id='+song.id
		})
  },

  tuiLogin(){
    wx.showModal({
		  content: '确认要退出?',
		  success: (res) => {
			if(res.confirm){
			  // 清空data中historyList
			  this.setData({
				historyList: []
			  })
			  // 登录信息
        wx.removeStorageSync('userInfo');
        wx.removeStorageSync('cookies');
        //   跳转到个人中心
        setTimeout(()=>{
          wx.reLaunch({
            url: '/pages/personal/personal'
            })
        }, 500)
			}
		  }
		})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
